﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConstantCMapEditor3
{
    public partial class AnimatedLightForm : Form
    {
        public AnimatedLightForm()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void apply_Click(object sender, EventArgs e)
        {
            applyToDeclaration(Game1.Selected);

            foreach (Declaration d in Game1.controlList) 
            {
                applyToDeclaration(d);
            }
        }

        private void applyToDeclaration(Declaration d) 
        {
         if (d != null) 
            {
                if (d is LightSourceComponent) 
                {
                    LightSourceComponent target = (LightSourceComponent)d;

                    target.RangeBase = (float)numericUpDownRangeBase.Value;
                    target.RangeDif = (float)numericUpDownRangeDif.Value;
                    target.RangeTime = (float)numericUpDownRangeTime.Value;


                    if (this.RangeType.SelectedIndex == 0) 
                    {
                        target.RangeModifierType = LightSourceComponent.ModifierType.Linear;
                    }
                    else if (this.RangeType.SelectedIndex == 1)                
                    {
                        target.RangeModifierType = LightSourceComponent.ModifierType.Sin;
                    }
                    else if (this.RangeType.SelectedIndex == 2)
                    {
                        target.RangeModifierType = LightSourceComponent.ModifierType.Cos;
                    }
                    else if (this.RangeType.SelectedIndex == 3)
                    {
                        target.RangeModifierType = LightSourceComponent.ModifierType.None;
                    }

                    target.IntensityBase = (float)numericUpDownIntensityBase.Value;
                    target.IntensityDif = (float)numericUpDownIntensityDif.Value;
                    target.IntensityTime = (float)numericUpDownIntensityTime.Value;


                    if (this.IntensityType.SelectedIndex == 0)
                    {
                        target.IntensityModifierType = LightSourceComponent.ModifierType.Linear;
                    }
                    else if (this.IntensityType.SelectedIndex == 1)
                    {
                        target.IntensityModifierType = LightSourceComponent.ModifierType.Sin;
                    }
                    else if (this.IntensityType.SelectedIndex == 2)
                    {
                        target.IntensityModifierType = LightSourceComponent.ModifierType.Cos;
                    }
                    else if (this.IntensityType.SelectedIndex == 3)
                    {
                        target.IntensityModifierType = LightSourceComponent.ModifierType.None;
                    }





                    target.ModifierReset();

                }
            }
        }

        public void Update(Declaration d)
        {
            if (d != null)
            {
                if (d is LightSourceComponent)
                {
                    LightSourceComponent target = (LightSourceComponent)d;



                    numericUpDownRangeBase.Value = (decimal)target.RangeBase;
                    numericUpDownRangeDif.Value = (decimal)target.RangeDif;
                    numericUpDownRangeTime.Value = (decimal)target.RangeTime;


                    if (target.RangeModifierType == LightSourceComponent.ModifierType.Linear)
                    {
                        this.RangeType.SelectedIndex = 0;
                    }
                    else if (target.RangeModifierType == LightSourceComponent.ModifierType.Sin)
                    {
                        this.RangeType.SelectedIndex = 1;
                    }
                    else if (target.RangeModifierType == LightSourceComponent.ModifierType.Cos)
                    {
                        this.RangeType.SelectedIndex = 2;
                    }
                    else if (target.RangeModifierType == LightSourceComponent.ModifierType.None)
                    {
                        this.RangeType.SelectedIndex = 3;
                    }

                    numericUpDownIntensityBase.Value = (decimal)target.IntensityBase;
                    numericUpDownIntensityDif.Value = (decimal)target.IntensityDif;
                    numericUpDownIntensityTime.Value = (decimal)target.IntensityTime;



                    if (target.IntensityModifierType == LightSourceComponent.ModifierType.Linear)
                    {
                        this.IntensityType.SelectedIndex = 0;
                    }
                    else if (target.IntensityModifierType == LightSourceComponent.ModifierType.Sin)
                    {
                        this.IntensityType.SelectedIndex = 1;
                    }
                    else if (target.IntensityModifierType == LightSourceComponent.ModifierType.Cos)
                    {
                        this.IntensityType.SelectedIndex = 2;
                    }
                    else if (target.IntensityModifierType == LightSourceComponent.ModifierType.None)
                    {
                        this.IntensityType.SelectedIndex = 3;
                    }




                }
            }
        }

        private void AnimatedLightForm_Load(object sender, EventArgs e)
        {
            //IntensityType.SelectedIndex = 0;
           // RangeType.SelectedIndex = 0;
        }

        private void numericUpDownIntensityBase_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown target = (NumericUpDown)sender;
            this.IntensityBaseTrackBar.Value = (int)((((float)(target.Value - target.Minimum)) / ((float)(target.Maximum - target.Minimum))) * 10000);
        }

        private void IntensityBaseTrackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar target = (TrackBar)sender;
            numericUpDownIntensityBase.Value =(decimal)(((float)(numericUpDownIntensityBase.Maximum - numericUpDownIntensityBase.Minimum)*target.Value/10000.0f)+(float)numericUpDownIntensityBase.Minimum);

        }

        private void numericUpDownIntensityDif_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown target = (NumericUpDown)sender;
            this.IntensityDifTrackBar.Value = (int)((((float)(target.Value - target.Minimum)) / ((float)(target.Maximum - target.Minimum))) * 10000);
        }

        private void IntensityDifTrackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar target = (TrackBar)sender;
            numericUpDownIntensityDif.Value = (decimal)(((float)(numericUpDownIntensityDif.Maximum - numericUpDownIntensityDif.Minimum) * target.Value / 10000.0f) + (float)numericUpDownIntensityDif.Minimum);
        }

        private void numericUpDownIntensityTime_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown target = (NumericUpDown)sender;
            this.IntensityTimeTrackBar.Value = (int)((((float)(target.Value - target.Minimum)) / ((float)(target.Maximum - target.Minimum))) * 10000);
        }

        private void IntensityTimeTrackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar target = (TrackBar)sender;
            numericUpDownIntensityTime.Value = (decimal)(((float)(numericUpDownIntensityTime.Maximum - numericUpDownIntensityTime.Minimum) * target.Value / 10000.0f) + (float)numericUpDownIntensityTime.Minimum);
        }





        //

        private void numericUpDownRangeBase_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown target = (NumericUpDown)sender;
            this.RangeBasetrackBar.Value = (int)((((float)(target.Value - target.Minimum)) / ((float)(target.Maximum - target.Minimum))) * 10000);
        }

        private void RangeBaseTrackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar target = (TrackBar)sender;
            numericUpDownRangeBase.Value = (decimal)(((float)(numericUpDownRangeBase.Maximum - numericUpDownRangeBase.Minimum) * target.Value / 10000.0f) + (float)numericUpDownRangeBase.Minimum);

        }

        private void numericUpDownRangeDif_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown target = (NumericUpDown)sender;
            this.RangeDiftrackBar.Value = (int)((((float)(target.Value - target.Minimum)) / ((float)(target.Maximum - target.Minimum))) * 10000);
        }

        private void RangeDifTrackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar target = (TrackBar)sender;
            numericUpDownRangeDif.Value = (decimal)(((float)(numericUpDownRangeDif.Maximum - numericUpDownRangeDif.Minimum) * target.Value / 10000.0f) + (float)numericUpDownRangeDif.Minimum);
        }

        private void numericUpDownRangeTime_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown target = (NumericUpDown)sender;
            this.RangeTimetrackBar.Value = (int)((((float)(target.Value - target.Minimum)) / ((float)(target.Maximum - target.Minimum))) * 10000);
        }

        private void RangeTimeTrackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar target = (TrackBar)sender;
            numericUpDownRangeTime.Value = (decimal)(((float)(numericUpDownRangeTime.Maximum - numericUpDownRangeTime.Minimum) * target.Value / 10000.0f) + (float)numericUpDownRangeTime.Minimum);
        }

        //







    }
}
